﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimalWars2
{
    public class Camera : Entity
    {
        public VisualizationMode m_VisualizationMode;

        public Camera(Game GameRef, Vector2 Pos, Texture2D Tex, VisualizationMode initialVisualizationMode)
            : base(GameRef, Pos, Tex)
        {
            m_VisualizationMode = initialVisualizationMode;
            m_Scale = 1.0f;

            m_Pos += new Vector2(0.0f, 1.0f) * 37.0f;
        }

        public override void Update(GameTime gameTime)
        {
            float DeltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            KeyboardState oKS = Keyboard.GetState();

            Vector2 vMoveDir = new Vector2(0.0f, 0.0f);

            if (oKS.IsKeyDown(Keys.L))
                vMoveDir.X = 1.0f;

            if (oKS.IsKeyDown(Keys.J))
                vMoveDir.X = -1.0f;

            if (oKS.IsKeyDown(Keys.I))
                vMoveDir.Y = -1.0f;

            if (oKS.IsKeyDown(Keys.K))
                vMoveDir.Y = 1.0f;

            if (vMoveDir.Length() > 0.0f)
                vMoveDir.Normalize();

            m_Pos += vMoveDir * 50.0f * DeltaTime;

            if (oKS.IsKeyDown(Keys.PageUp))
            {
                m_Scale -= 0.25f * DeltaTime;
            }

            if (oKS.IsKeyDown(Keys.PageDown))
            {
                m_Scale += 0.25f * DeltaTime;
            }

            //if (oKS.IsKeyDown(Keys.Space)) {

            //  m_Scale = 1.0f + 0.1f * (float)Math.Sin( 6.0f * Math.PI * gameTime.TotalGameTime.TotalSeconds );
            // }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Vector2 vCameraScale = new Vector2(0.05f, 0.05f);

            Vector2 vCameraSize = new Vector2(m_Game.m_DebugCircle.Width,
                                              m_Game.m_DebugCircle.Height);

            spriteBatch.Draw(m_Game.m_DebugCircle,
                             TransformPos(m_Pos), null, new Color(0.0f, 0.0f, 1.0f, 0.1f), 0.0f,
                             vCameraSize * 0.5f,
                             TransformScale(m_Pos, vCameraScale), SpriteEffects.None, 0.0f);
        }

        public Vector2 TransformPos(Vector2 vPos)
        {
            if (m_VisualizationMode == VisualizationMode.Perspective)
            {
                Vector2 vOffset;
                vOffset = vPos - m_Pos + new Vector2(0.0f, 80.0f);

                float fAux = (Constants.TABLE_RIGHT_LIMIT - vOffset.Y) / (Constants.TABLE_RIGHT_LIMIT * 2.0f);
                fAux = 1.0f - fAux;
                //fAux = 0.5f + 0.5f * fAux;
                fAux = 0.8f + 1.2f * fAux;
                vOffset.X *= fAux;
                vOffset.Y *= 0.5f;

                return new Vector2(Constants.WINDOW_WIDTH * 0.5f, Constants.WINDOW_HEIGHT * 0.5f) + vOffset * (m_Scale + 0.45f);
            }
            else 
            { 
                Vector2 vOffset;
                vOffset = vPos - m_Pos; 

                return new Vector2(Constants.WINDOW_WIDTH * 0.5f, Constants.WINDOW_HEIGHT * 0.5f) + vOffset * m_Scale;
            }
        }

        public Vector2 TransformScale( Vector2 vPos, Vector2 vScale )
        {
            if (m_VisualizationMode == VisualizationMode.Perspective)
            {
                Vector2 vOffset = vPos - m_Pos + new Vector2(0.0f, 120.0f);

                float fAux = (Constants.TABLE_RIGHT_LIMIT - vOffset.Y) / (Constants.TABLE_RIGHT_LIMIT * 2.0f);
                fAux = 1.0f - fAux;
                //fAux = 0.5f + 0.5f * fAux;
                //fAux = 0.2f + 0.8f * fAux;
                fAux = 0.8f + 1.2f * fAux;
                vScale *= fAux;

                return vScale * (m_Scale); // + 0.45f
            }
            else
            {
                return vScale * m_Scale;
            }
        }
    }
}
